package com.common.valid;



import com.common.Constants;
import com.common.valid.annotation.UserName;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserNameValidator implements ConstraintValidator<UserName,String> {
    private static final Pattern PHONE_PATTERN = Pattern.compile(Constants.VALID_PHONE_PATTERN);
    private static final Pattern EMAIL_PATTERN = Pattern.compile(Constants.VALID_EMAIL_PATTERN);

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.isNotBlank(value)){
            Matcher m = PHONE_PATTERN.matcher(value);
            if(m.find()) return true;
            m = EMAIL_PATTERN.matcher(value);
            return m.find();
        }
        return false;
    }
}
